package com.huilink.webmail.util.client.widgets;

import org.gwm.client.impl.DefaultGFrame;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class MyDialog extends DefaultGFrame{

	private HorizontalPanel panel = new HorizontalPanel();
	private VerticalPanel rightPanel = new VerticalPanel();
	private HorizontalPanel buttonPanel = new HorizontalPanel();
	
	private Image icon;
	
	private Button yes;
	private Button cancel;
	
	public MyDialog(String message, String url, int width, int height, ClickListener clickListener){
		this(message, url, width, height, clickListener, false);
	}
	
	public MyDialog(String message, String url, int width, int height, ClickListener clickListener, boolean isOnlyMessage)
	{
		setCaption("");
		
		if(url != null){
			setIcon(url, width, height);
			panel.add(icon);
			panel.setCellVerticalAlignment(icon, HasVerticalAlignment.ALIGN_MIDDLE);
		}
		panel.setSpacing(8);
		rightPanel.add(new Label(message, false));
		rightPanel.setSpacing(8);
		buttonPanel.setSpacing(8);
		yes = new Button("yes");
		yes.setWidth("60");
		buttonPanel.add(yes);
		
		if (clickListener != null)
			yes.addClickListener(clickListener);
		
		yes.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				close();
			}
		});
		
		if (!isOnlyMessage)
		{
			cancel = new Button("cancel");
			cancel.setWidth("60");
			buttonPanel.add(cancel);
			cancel.addClickListener(new ClickListener(){
				public void onClick(Widget sender) {
					close();
				}
			});
		}
		buttonPanel.setWidth("70%");
		rightPanel.add(buttonPanel);
		rightPanel.setCellHorizontalAlignment(buttonPanel, HasHorizontalAlignment.ALIGN_CENTER);
		panel.add(rightPanel);
		
		setContent(panel);
		
		DOM.addEventPreview(this);
		setClosable(false);
        setMaximizable(false);
        setMinimizable(false);
	}

	
	private void setIcon(String url, int width, int height){
		icon = new Image(url);
		icon.setWidth(width + "px");
		icon.setHeight(height + "px");
	}
	
	public void show(){
		
	}
	
    public boolean onEventPreview(Event event) {
    	int type = DOM.eventGetType(event);
        switch (type) {

	        case Event.ONMOUSEDOWN:
	        case Event.ONMOUSEUP:
	        case Event.ONMOUSEMOVE:
	        case Event.ONCLICK:
	        case Event.ONDBLCLICK: {
	            if (DOM.getCaptureElement() == null) {
	                Element target = DOM.eventGetTarget(event);
	                if (!DOM.isOrHasChild(getElement(), target) && type == Event.ONCLICK) {
	                	close();
	                    return true;
	                }
	            }
	            break;
	        }
	        default:
	        	return true;
        }
        return true;
    }
    
    public void close(){
    	DOM.removeEventPreview(this);
    	super.close();
    }
}
